<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * Record
 * @package	Codeigniter
 * @author	Chhing Chhing
 * @link	chhingchhing.hem@gmail.com
 */

class Staff extends PNC_Controller {
    /*
     * Constructor
     */

    public function __construct() {
        parent::__construct();
        $this->check_access();
        $this->load->model(array("mod_staff", "mod_leave_request", "mod_option", "mod_record"));
        $this->view_data['base_url'] = base_url();
    }

    /*
     * Index
     * @access public
     * @return void
     */

    public function index() {
        redirect('staff/home/');
    }

    /*
     * Show Record
     * @access public
     * @return void
     */

    public function home() {
        if ($this->check_session()) {
            $data['title'] = "Staff";
            $session_id = $this->session->userdata('id_user');
            $data['datas'] = $this->mod_staff->selectdata($session_id);
            $data['total'] = $this->mod_staff->total($session_id);
            $this->view_main($data);
        } else {
           $this->view_main($data); //////====Go without login=========
//           redirect('main/');
        }
    }

    public function leaving() {
        if ($this->check_session()) {
            $data['title'] = "Do Request";
            $data['validator'] = $this->mod_option->get_manage_mail('manager', $this->session->userdata('staff_id'));
            $data['super'] = $this->mod_leave_request->super_type_leave();
            $data['type'] = $this->mod_leave_request->type_leave();
            $data['sub_type'] = $this->mod_leave_request->sub_type_leave();
            $data['sub_type_per'] = $this->mod_leave_request->sub_type_leave_per();
            if ($this->input->post('btn_do_request')) {
                $config = array(
                    array(
                        'field' => 'duration',
                        'label' => 'Time Duration',
                        'rules' => 'trim|required|xss_clean'
                    ),
                    array(
                        'field' => 'reason',
                        'label' => 'Reason',
                        'rules' => 'trim|required|xss_clean'
                    ),
                    array(
                        'field' => 'super_type',
                        'label' => 'Type of Leave',
                        'rules' => 'trim|required|greater_than[0]'
                    ),
                    array(
                        'field' => 'validators',
                        'label' => 'Manager Mail',
                        'rules' => 'trim|required|valid_email'
                    ),
                    array(
                        'field' => 's_date',
                        'label' => ' Start Date',
                        'rules' => 'trim|required|callback_compareDates'
                    ),
                    array(
                        'field' => 'e_date',
                        'label' => 'End Date',
                        'rules' => 'trim|required|callback_compareDates'
                    )
                );
                $this->form_validation->set_rules($config);
                $this->form_validation->set_message('greater_than[0]', 'The field must be select.');
                if ($this->form_validation->run() === FALSE) {
                    $this->session->set_userdata('validate_fail', show_message('Please check your completed form!', 'error'));
                    $this->view_main($data);
                } else {
                    $staff = $this->session->userdata('staff_id');
                    $validator = $this->input->post('validators');
                    $super_type = $this->input->post('super_type');
                    $sub_type = $this->input->post('sub');
                    $sub_sub = $this->input->post('sub_sub');
                    $desc = $this->input->post('desc_up');
                    $work_on = $this->input->post('working_on');
                    $catch_on = $this->input->post('catchup_on');
                    $reason = $this->input->post('reason');
                    $start_date = $this->input->post('s_date');
                    $end_date = $this->input->post('e_date');
                    $hour = $this->input->post('duration');

                    $this->session->set_userdata('reciever_mail', $validator);
                    $this->session->set_userdata('reason_leaving', $reason);

                    $day = ($hour / 8);

                    if ($start_date <= $end_date) {
                        $success_request = '';
                        if ($super_type == 1) {
                            $success_request = PNC_Model::add_record($staff, $validator, $super_type, $sub_type, $sub_sub, '', $reason, $start_date, $end_date, $hour, $day, '', '');
                            $this->send_mail_request('staff', 'staff', 'home', 'leaving');
                            if ($success_request > 0) {
                                $this->send_mail_request('staff', 'staff', 'home', 'leaving');
                                
                            } else {
                                $this->session->set_userdata('send_request_erro', show_message('Sending email for do request was failed!', 'error'));
                                $this->view_main($data);
                            }
                        } else if ($super_type == 2) {
                            $success_request = PNC_Model::add_record($staff, $validator, $super_type, '', '', $desc, $reason, $start_date, $end_date, $hour, $day, $work_on, $catch_on);
                            if ($success_request > 0) {
                                $this->send_mail_request('staff', 'staff', 'home', 'leaving');
                            } else {
                                $this->session->set_userdata('sending_erro', show_message('Can not do request leaving!', 'error'));
                                $this->view_main($data);
                            }
                        }
                    } else {
                        $this->session->set_userdata('sent_erro', show_message('Start date must be smaller than end date!', 'error'));
                        $this->view_main($data);
                    }
                }
            } else {
                $this->view_main($data);
            }
        }
    }

    public function get_profile() {
        if ($this->check_session()) {
            $data['title'] = 'Profile';
            $data['get_profile'] = PNC_Model::get_profile($this->session->userdata('staff_id'));
            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    /* Change new password */

    public function change_password() {
        if ($this->check_session() AND $this->session->userdata('staff')) {
            if ($this->input->post('btn_save_change')) {
                $old_pass = $this->input->post('old_pass');
                $new_pass = $this->input->post('new_pass');
                $con_pass = $this->input->post('con_pass');
                $old_pass = md5($old_pass);
                $pass = $this->session->userdata('old_pass');

                if ($old_pass === '' OR $new_pass === '' OR $con_pass === '') {
                    $this->session->set_userdata('input_blank', show_message('Please input all fields!', 'error'));
                    redirect('staff/change_pass');
                } else if (strlen($new_pass) < 5 OR strlen($new_pass) > 15) {
                    $this->session->set_userdata('min_length', show_message('Password must be longer than 5 and shorter than 15 characters!', 'error'));
                    redirect('record/change_pass');
                } else if ($old_pass != $pass) {
                    $this->session->set_userdata('opass_err', show_message('Your old password is not match!', 'error'));
                    redirect('staff/change_pass');
                } else if ($new_pass != $con_pass) {
                    $this->session->set_userdata('npass_err', show_message('New password and confirm password is not match!', 'error'));
                    redirect('staff/change_pass');
                } else {
                    $new_pass = md5($new_pass);
                    PNC_Model::change_password($old_pass, $new_pass, $con_pass, $this->session->userdata('staff_id'));
                    $this->session->set_userdata('change_success', show_message('Changed password successfully!', 'success'));
                    redirect('staff/get_profile');
                }
            }
        } else {
            echo 'helo nothing';
            die();
            redirect('main/');
        }
    }

    public function change_pass() {
        $data['title'] = 'Change Password';
        if ($this->input->post('btn_save_change')) {
            $this->change_password();
            redirect('staff/get_profile');
        }
        $this->view_main($data);
    }

    public function employee_profil() {
        if ($this->check_session()) {
            $data['menu'] = $this->mod_menu->show_menu();
            $data['title'] = "Profile";
            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    function info() {
        if ($this->check_session()) {
            $staff_id = $this->session->userdata('staff_id');
            $data['title'] = 'Personal Leave Request';
            $data['personal'] = $this->mod_staff->personal_leave($staff_id);
            $data['query'] = $this->mod_staff->personal_leave($staff_id);
            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    public function update_before_approve() {
        if ($this->check_session()) {
            $this->session->set_userdata('old_lea_id', $this->uri->segment(3));
            $this->session->set_userdata('old_desc_id', $this->uri->segment(5));
            $this->session->set_userdata('rcd_id', $this->uri->segment(4));
            if ($this->uri->segment(3) == 1) {
                $data['edit_annual'] = PNC_Model::show_detail($this->uri->segment(4), 'annual');
            } else if ($this->uri->segment(3) == 2) {
                $data['edit_catchup'] = PNC_Model::show_detail($this->uri->segment(4), 'catchup');
            }
            $data['title'] = 'Edit Leaving Request';
            $data['hour'] = array('' => '-- Select --', '4' => '4 Hours', '8' => '8 Hours', '12' => '12 Hours', '16' => '16 Hours');
            $data['rcd_status'] = array('' => '-- Select --', '0' => 'Disapprove', '1' => 'Approve');
            $data['leave_type'] = $this->mod_option->get_leaving_type();
            $data['sub_type'] = $this->mod_option->get_sub_leaving();
            $data['validator'] = $this->mod_option->get_validator();
            $data['ssl_type'] = $this->mod_option->get_ssl_type();
            if ($this->input->post('btn_update')) {
                $old_lea_id = $this->input->post('old_lea_id');
                $old_desc_id = $this->input->post('old_desc_id');
                $record_id = $this->input->post('record_id');
                /* Old post from form update */
                $old_sub_type = $this->input->post('old_sub_type');
                $old_ss_type = $this->input->post('old_ss_type');
                $old_reason = $this->input->post('old_reason');
                $old_start_date = $this->input->post('old_s_date');
                $old_end_date = $this->input->post('old_e_date');
                $old_desc_up = $this->input->post('old_desc_up');
                $old_working_on = $this->input->post('old_working_on');
                $old_catchup_on = $this->input->post('old_catchup_on');
                $old_hour = $this->input->post('old_duration');
                /* Check and Compare value */
                $supper_type = !$this->input->post('super_type') ? $old_lea_id : $this->input->post('super_type');
                $sub_type = !$this->input->post('sub_type') ? $old_sub_type : $this->input->post('sub_type');
                $ss_type = !$this->input->post('ss_type') ? $old_ss_type : $this->input->post('ss_type');
                $reason = !$this->input->post('txt_reason') ? $old_reason : $this->input->post('txt_reason');
                $start_date = !$this->input->post('s_date') ? $old_start_date : $this->input->post('s_date');
                $end_date = !$this->input->post('e_date') ? $old_end_date : $this->input->post('e_date');
                $desc_up = !$this->input->post('desc_up') ? $old_desc_up : $this->input->post('desc_up');
                $working_on = !$this->input->post('working_on') ? $old_working_on : $this->input->post('working_on');
                $catchup_on = !$this->input->post('catchup_on') ? $old_catchup_on : $this->input->post('catchup_on');
                $hour = !$this->input->post('duration') ? $old_hour : $this->input->post('duration');
                $days = ($hour / 8);
//				echo 'Sub type '.$sup_type; die();
                $result = '';
                /* If update with change leave type */
                if ($supper_type != $old_lea_id) {
                    if ($supper_type == 1) {
                        PNC_Model::delete_datas('desc_id', $old_desc_id, '', 'pnc_cup_des');
                        $result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
                    } else if ($supper_type == 2) {
                        $result = PNC_Model::update_catchup_before($record_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
                    }
                } else {
                    if ($supper_type == 1) {
                        $result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
                    } else if ($supper_type == 2) {
                        $result = PNC_Model::update_catchup($record_id, $old_desc_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
                    }
                }
                if ($result > 0) {
                    $this->session->set_userdata('edit_success', show_message('Edited successfully!', 'success'));
                    if ($this->session->userdata('per_page')) {
                        redirect('staff/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
                    }
                    redirect('staff/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
                } else {
                    $this->session->set_userdata('edit_erro', show_message('Editing was fail!', 'error'));
                    redirect('staff/info/update_before_approve/' . $this->session->userdata('pagination'));
                }
            } else {
                $this->view_main($data);
            }
        } else {
            redirect('main/');
        }
    }

    public function delete_before_approve() {

        if ($this->uri->segment(3) == 1) {
            $this->session->set_userdata('get_page', $this->uri->segment(3));
            $this->session->set_userdata('get_id', $this->uri->segment(4));
            $page = $this->session->userdata('get_page');
            $id = $this->session->userdata('get_id');
            $this->mod_record->delete_once($id, 'rcd_id', 'pnc_record', '');
            $this->session->set_userdata('delete_success', show_message('Deleted successfully!', 'success'));
            if ($this->session->userdata('per_page')) {
                redirect('staff/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
            }
            redirect('staff/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
        } else if ($this->uri->segment(3) == 2) {
            $this->session->set_userdata('page', $this->uri->segment(4));
            $pages = $this->session->userdata('page');
            $ts = $this->uri->total_segments(); /* Get last segment of uri */
            $desc_id = $this->uri->segment($ts);
            $id = $this->uri->segment($ts - 1);
            $this->mod_record->delete_once($id, 'rcd_id', 'pnc_record', $desc_id);
            $this->session->set_userdata('delete_success', show_message('Deleted successfully!', 'success'));
            if ($this->session->userdata('per_page')) {
                redirect('staff/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
            }
            redirect('staff/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
        }
    }

    /* a
     * Logout Admin
     * @return void
     * @access public
     */

    public function logout_staff() {
        $this->session->set_userdata('logout', show_message('You have been logout!', 'notice'));
        $this->session->unset_userdata('staff');
        redirect("main/");
    }

}

/* End of file staff.php */
/* Location: ./application/models/staff.php */